package tool

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"log"
)

var DbEngine *Orm

type Orm struct {
	*xorm.Engine
}

func OrmEngine(cfg *Config) (*Orm, error) {
	database := cfg.Database
	conn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s",
		database.User,
		database.Password,
		database.Host,
		database.Port,
		database.DbName,
		database.Charset,
		)

	log.Printf("conn is %s", conn)

	//创建Orm引擎
	engine, err := xorm.NewEngine(database.Driver, conn)
	if err != nil {
		return nil, err
	}

	//是否大于sql
	engine.ShowSQL(database.ShowSql)

	orm := new(Orm)
	orm.Engine = engine
	DbEngine = orm
	return orm, nil
}
